package com.me.mygdxgame;

import java.awt.geom.CubicCurve2D;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

public class MyGdxGame implements ApplicationListener {
	 private Mesh squareMesh;
	    private Mesh nearSquare;
	    private Camera camera;
	    private Texture texture;

	    @Override
	    public void create() {
	        if (squareMesh == null) {
	            squareMesh = new Mesh(true, 8, 8, 
	                    new VertexAttribute(Usage.Position, 3, "a_position"),
	                    new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
	                    new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCords"));

	            squareMesh.setVertices(new float[] {
	                    0, -0f, 0, Color.toFloatBits(128, 0, 0, 255), 0f,0f,
	                    1, -0f, 0, Color.toFloatBits(192, 0, 0, 255),1f,0f,
	                    0, 1f, 0, Color.toFloatBits(192, 0, 0, 255),0,1f,
	                    1, 1f, 0, Color.toFloatBits(255, 0, 0, 255), 1f,1f,   
	            		0, -0f, 1, Color.toFloatBits(128, 0, 0, 255), 0f,0f,
	            		1, -0f, 1, Color.toFloatBits(192, 0, 0, 255),1f,0f,
	            		0, 1f, 1, Color.toFloatBits(192, 0, 0, 255),0,1f,
	            		1, 1f, 1, Color.toFloatBits(255, 0, 0, 255), 1f,1f });   
	            squareMesh.setIndices(new short[] { 0, 1, 2, 3, 1, 4, 5,6});
	        }

	        if (nearSquare == null) {
	            nearSquare = new Mesh(true, 4, 4, 
	                    new VertexAttribute(Usage.Position, 3, "a_position"),
	                    new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
	                    new VertexAttribute(Usage.TextureCoordinates, 2, "a_texssCords"));

	            nearSquare.setVertices(new float[] {
	                    -1, -0.5f, -1.1f, Color.toFloatBits(0, 0, 128, 255), 0f,0f,
	                    0, -0.5f, -1.1f, Color.toFloatBits(0, 0, 192, 255),1f,0f,
	                    -1, 0.5f, -1.1f, Color.toFloatBits(0, 0, 192, 255),0,1f,
	                    0, 0.5f, -1.1f, Color.toFloatBits(0, 0, 255, 255),1f,1f  });   
	            nearSquare.setIndices(new short[] { 0, 1, 2, 3});
	        }
	        
			texture = new Texture("data/libgdx.png");
	    }

	    @Override
	    public void dispose() { }

	    @Override
	    public void pause() { }

	    int counter = 0;
	    float sign = -1;
	    @Override
	    public void render() {
	        
//	        camera.rotate(sign*1f,0, 0, 1f);
	        counter += sign;
	        if (counter>360 || counter<0) 
	        {
	        	sign = -sign;
	            System.out.println(Gdx.graphics.getFramesPerSecond());
	        }
//	        
	    	camera.update();
	        camera.apply(Gdx.gl10);
	        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	        Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);
	        Gdx.gl.glEnable(GL10.GL_LINEAR_MIPMAP_LINEAR);
	        texture.bind();
	        Gdx.gl10.glPushMatrix();
	        Gdx.gl10.glRotatef(counter*sign, 0f, 1f, 0);
	        //Gdx.gl10.glTranslatef(0, 0, -10);
	        squareMesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4);
	        
	   
	        Gdx.gl10.glPopMatrix();
	        //nearSquare.render(GL10.GL_TRIANGLE_STRIP, 0, 4);
	    }

	    @Override
	    public void resize(int width, int height) {
	        float aspectRatio = (float) width / (float) height;
	        camera = new PerspectiveCamera(45, 16f * aspectRatio, 16f);
	        
	        camera.translate(0, 10, 0);
	        camera.lookAt(0, 0, 0);
	    }

	    @Override
	    public void resume() { }
}
